package com.ruoyi.web.controller.consume;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.consume.domain.SeedApplyCon;
import com.ruoyi.consume.service.ISeedApplyConService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 种子领用Controller
 *
 * @author ruoyi
 * @date 2021-07-22
 */
@Api("种子领用")
@RestController
@RequestMapping("/consume/seedap")
public class SeedApplyConController extends BaseController
{
    @Autowired
    private ISeedApplyConService seedApplyService;

    /**
     * 查询种子申请领用列表
     */
    @ApiOperation("查询种子申请领用列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:seedap:list')")
    @GetMapping("/list")
    public TableDataInfo list(SeedApplyCon seedApplyCon)
    {
        startPage();
        List<SeedApplyCon> list = seedApplyService.selectSeedApplyList(seedApplyCon);
        return getDataTable(list);
    }

    /**
     * 导出种子领用列表
     */
    @PreAuthorize("@ss.hasPermi('consume:seedap:export')")
    @Log(title = "种子领用", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SeedApplyCon seedApplyCon)
    {
        List<SeedApplyCon> list = seedApplyService.selectSeedApplyList(seedApplyCon);
        ExcelUtil<SeedApplyCon> util = new ExcelUtil<SeedApplyCon>(SeedApplyCon.class);
        return util.exportExcel(list, "种子领用数据");
    }

    /**
     * 获取种子领用详细信息
     */
    @PreAuthorize("@ss.hasPermi('consume:seedap:query')")
    @GetMapping(value = "/{seedApplyId}")
    public AjaxResult getInfo(@PathVariable("seedApplyId") Long seedApplyId)
    {
        return AjaxResult.success(seedApplyService.selectSeedApplyById(seedApplyId));
    }

    /**
     * 新增种子领用
     */
    @PreAuthorize("@ss.hasPermi('consume:seedap:add')")
    @Log(title = "种子领用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SeedApplyCon seedApplyCon)
    {
        return toAjax(seedApplyService.insertSeedApply(seedApplyCon));
    }

    /**
     * 修改种子领用
     */
    @PreAuthorize("@ss.hasPermi('consume:seedap:edit')")
    @Log(title = "种子领用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SeedApplyCon seedApplyCon)
    {
        return toAjax(seedApplyService.updateSeedApply(seedApplyCon));
    }

    /**
     * 删除种子领用
     */
    @PreAuthorize("@ss.hasPermi('consume:seedap:remove')")
    @Log(title = "种子领用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{seedApplyIds}")
    public AjaxResult remove(@PathVariable Long[] seedApplyIds)
    {
        return toAjax(seedApplyService.deleteSeedApplyByIds(seedApplyIds));
    }

    /**
     * 更新库存量表
     */
    @ApiOperation("更新库存量表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:seedap:amount')")
    @Log(title = "种子领用", businessType = BusinessType.UPDATE)
    @PutMapping("/amount")
    public AjaxResult amount(@RequestBody SeedApplyCon seedApplyCon)
    {
        int row = seedApplyService.updateSeedAmount(seedApplyCon);
        if(row == 0){
            return error("库存不足");
        }
        else {
            return toAjax(row);
        }
    }

    /**
     * 更新领用状态
     */
    @ApiOperation("更新领用状态")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:seedap:consu')")
    @Log(title = "种子领用", businessType = BusinessType.UPDATE)
    @PutMapping("/consu/{seedApplyId}")
    public AjaxResult consume(@PathVariable("seedApplyId") Long seedApplyId)
    {
        return toAjax(seedApplyService.updateConsumeStatus(seedApplyId));
    }

    /**
     * 退回操作
     */
    @ApiOperation("退回操作")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:seedap:back')")
    @Log(title = "种子领用", businessType = BusinessType.UPDATE)
    @PutMapping("/back/{seedApplyId}")
    public AjaxResult back(@PathVariable("seedApplyId") Long seedApplyId)
    {
        return toAjax(seedApplyService.updateApprovalStatus(seedApplyId));
    }
}
